package com.coffeeloversupportlibrary.coffeelover.domain.article.adapter;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.contentshow.Details;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.PopularType;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.Game;
import com.coffeeloversupportlibrary.coffeelover.mercury.entity.User;
import com.coffeeloversupportlibrary.coffeelover.mercury.game.FirstStageActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.game.GameStartActivity;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.HttpUtils;
import com.coffeeloversupportlibrary.coffeelover.mercury.utils.JsonUtils;
import com.coffeeloversupportlibrary.databinding.PopularShowBinding;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author puyv
 * @createTime 2021/11/6 20:17
 * @projectName CoffeeLoverSupportLibrary
 * @className PopularAdapter.java
 * @description TODO
 *   mDiariesBinding = FragmentDiariesBinding.inflate(inflater, container, false);
 *    PopularShowBinding
 */
public class PopularAdapter implements Adapter<List<PopularType>>{
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.popular_show,parent,false);
        PopularShowBinding popularShowBinding=PopularShowBinding.bind(view);
        return new PopularArticleViewHolder(popularShowBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull List<PopularType> items, @NonNull RecyclerView.ViewHolder holder, int position) {
        ((PopularArticleViewHolder)holder).popularShowBinding
                .clickable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpUtils httpUtils = new HttpUtils();
                JsonUtils jsonUtils = new JsonUtils();
                SharedPreferences userInfo =  holder.itemView.getContext().getSharedPreferences("user", 0);
                User user = jsonUtils.getUser(userInfo.getString("userInfo" ,""));
                String userId = user.getData().getUser().getUserId();
                //暂时是游戏
                Call call = httpUtils.getCall("/tree/tree/"+userId);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String res = response.body().string();
                        Log.e("Main", "onResponse: " + res);
                        Game game = jsonUtils.getGame(res);
                        SharedPreferences.Editor editor = userInfo.edit();
                        editor.putString("tree", res);
                        editor.commit();
                        if (game.getData().getTree() == null) {
                            Intent intent = new Intent(holder.itemView.getContext(), GameStartActivity.class);
                            holder.itemView.getContext().startActivity(intent);
                        } else{
                            Intent intent = new Intent(holder.itemView.getContext(), FirstStageActivity.class);
                            holder.itemView.getContext().startActivity(intent);
                        }
                    }
                });
            }
        });

    }
    static class PopularArticleViewHolder extends RecyclerView.ViewHolder{
        private PopularShowBinding popularShowBinding;
        public PopularArticleViewHolder(@NonNull PopularShowBinding popularShowBinding) {
            super(popularShowBinding.getRoot());
            this.popularShowBinding=popularShowBinding;
        }
    }

}
